package com.ldr.danmuku.item

import android.content.Context
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.view.View.MeasureSpec
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.ldr.danmuku.DanMuConfig
import com.ldr.danmuku.DanMuLaneView
import com.ldr.danmuku.DanMuManager

/**
 * @author :create by ldr
 * time:2022/12/28 16:25
 * @descript:文字类型的弹幕
 */
class DanMuTextItemView(var mDanMuTextBean:DanMuTextBean,var context: Context) :DanMuItemView,
    View.OnLayoutChangeListener, View.OnTouchListener, View.OnAttachStateChangeListener {

    private var mTextView:TextView?=null
    //记录当前View的宽高
    var mCurrentWidthAndHeightPair = Pair(0f,0f)
    //弹幕库的配置
    var mDanMuConfig:DanMuConfig = DanMuManager.singleton.mDanMuConfig

    var mEndPosition = 0

    companion object{
        private const val TAG = "DanMuTextItemView"
    }

    override val mDanmuItemTypeEnum: DanMuItemTypeEnum
        get() = DanMuItemTypeEnum.TEXT

    override fun getView(): View {
        return getTextView()
    }

    /**
     * 计算本身的宽高
     */
    override fun judgeItSelfHeightAndWidth(): Pair<Float, Float> {
        // 测量子控件
        val width = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
        val height = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
        mTextView?.measure(width,height)
        val measuredHeight  = mTextView?.measuredHeight?:0
        val measuredWidth  = mTextView?.measuredWidth?:0
        mCurrentWidthAndHeightPair = Pair(MeasureSpec.getSize(measuredWidth).toFloat(),MeasureSpec.getSize(measuredHeight).toFloat())
        return mCurrentWidthAndHeightPair
    }

    private fun getTextView():TextView{
        if (mTextView != null){
             mTextView!!.apply {
                text = mDanMuTextBean.content
                mDanMuTextBean.contentColorRes?.let {
                    setTextColor(ContextCompat.getColor(context,it))
                }
            }
             addViewListener()
            judgeItSelfHeightAndWidth()
            return mTextView!!
        } else {
          TextView(context).apply {
              text = mDanMuTextBean.content?:" " //给个空字符方便测量高度
              setSingleLine()
              setPadding(mDanMuConfig.mDanMuItemMarginHorizontal.toInt(),0,mDanMuConfig.mDanMuItemMarginHorizontal.toInt(),0)
              layoutParams = ViewGroup.LayoutParams(
                  ViewGroup.LayoutParams.WRAP_CONTENT,
                  ViewGroup.LayoutParams.WRAP_CONTENT
              )
              if (mDanMuTextBean.contentColorRes == null) {
                  setTextColor(ContextCompat.getColor(context, mDanMuConfig.mDanMuNormalTextColor))
              } else {
                  setTextColor(ContextCompat.getColor(context, mDanMuTextBean.contentColorRes!!))
              }
              textSize = mDanMuConfig.mTextSize
              mTextView = this
          }
            judgeItSelfHeightAndWidth()
            addViewListener()
            return mTextView!!
        }
    }

    fun addViewListener(){
        mTextView?.addOnLayoutChangeListener(this)
        mTextView?.addOnAttachStateChangeListener(this)
    }

    override fun release() {
        mEndPosition = 0
        mTextView?.removeOnLayoutChangeListener(this)
    }

    override fun onLayoutChange(
        v: View?,
        left: Int,
        top: Int,
        right: Int,
        bottom: Int,
        oldLeft: Int,
        oldTop: Int,
        oldRight: Int,
        oldBottom: Int
    ) {
        Log.d("Danmu", "onLayoutChange: $left $top $right $bottom $oldLeft $oldTop $oldRight $oldBottom")

    }

    override fun onTouch(v: View?, event: MotionEvent?): Boolean {
        return false
    }

    /**
     * 更新当前View的位置
     */
    override fun updateCurrentLocalLayout(){
       val intervalDistance =  DanMuManager.singleton.intervalDistance
       mTextView?.apply {
           layout(
               left - intervalDistance.toInt(),
               top,
               left - intervalDistance.toInt() + mCurrentWidthAndHeightPair.first.toInt(),
               top + mCurrentWidthAndHeightPair.second.toInt())
           mEndPosition = left - intervalDistance.toInt() + mCurrentWidthAndHeightPair.first.toInt()
           requestLayout()
       }
    }

    /**
     * 获取view的end的位置
     */
    override fun getLocalEndPosition(): Int {
        return mEndPosition
    }

    /**
     * 设置初始化的位置
     */
    override fun setFirstLocalLayout(v: View){
       v.parent?.let {
           val danMuLaneView = it as DanMuLaneView
           val parentWidth = danMuLaneView.width
           mTextView?.apply {
               layout(parentWidth,
                   mDanMuConfig.mLanViewPaddingTop.toInt(),
                   parentWidth + mCurrentWidthAndHeightPair.first.toInt(),
                   mDanMuConfig.mLanViewPaddingTop.toInt() + mCurrentWidthAndHeightPair.second.toInt(), //padding + 本身高度
               )
               mEndPosition = parentWidth + mCurrentWidthAndHeightPair.first.toInt()
               requestLayout()
           }
       }
    }

    override fun onViewAttachedToWindow(v: View) {
        setFirstLocalLayout(v)
    }

    override fun onViewDetachedFromWindow(v: View) {
        release()
        DanMuManager.singleton.mDanMuTextItemViewPool.release(this@DanMuTextItemView)
    }

}
